package studychinese.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.utils.ToastUtils;
import studychinese.utils.VerifyUtils;

public class RegisterActivity extends BaseAcitivity {


    @Bind(R.id.et_register_account)
    EditText etRegisterAccount;
    @Bind(R.id.rl_registera_acount)
    RelativeLayout rlRegisteraAcount;
    @Bind(R.id.tv_register_point)
    TextView tvRegisterPoint;
    @Bind(R.id.rl_register_back)
    RelativeLayout rlRegisterBack;
    @Bind(R.id.rl_rigister_next)
    RelativeLayout rlRigisterNext;
    @Bind(R.id.btn_register_goregister)
    Button btnRegisterGoregister;
    @Bind(R.id.iv_login_login)
    ImageView iv_login_login;

    @Override
    protected int setContainer() {
        return R.layout.activity_register1;
    }

    @Override
    public void innitView() {
        ButterKnife.bind(this);
        setIsFocusabale(false);
        etRegisterAccount.setOnFocusChangeListener(new View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    tvRegisterPoint.setVisibility(View.GONE);
                } else {
                    // 此处为失去焦点时的处理内容
                    if(!VerifyUtils.isEmailNO(etRegisterAccount.getText().toString().trim())){
                        tvRegisterPoint.setVisibility(View.VISIBLE);
                    }else {
                        tvRegisterPoint.setVisibility(View.GONE);
                    }
                }
            }
        });
        etRegisterAccount.addTextChangedListener(textWatcher);
    }



    @OnClick({ R.id.btn_register_goregister, R.id.iv_login_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {

            case R.id.btn_register_goregister:
                startActivity(new Intent(this,LoginActivity.class).putExtra("input",getIntent().getStringExtra("input")));
                this.finish();
                break;


            case R.id.iv_login_login:
                String account = etRegisterAccount.getText().toString().trim();
                if("".equals(account)){
                    ToastUtils.showShort("账号不能为空");
                }else if(!VerifyUtils.isEmailNO(account)){
                    tvRegisterPoint.setVisibility(View.VISIBLE);
                }else {
                    setVerification(account);

                }


                break;
        }
    }
    // 验证接口
    private void setVerification(final String account) {
        OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_GETUSERPWD)).params("user_name",account).
                execute(new DialogCallback<String>(this,false) {
            @Override
            public void onSuccess(Response<String> response) {
                try {
                    JSONObject obj = new JSONObject(response.body());
                    String  state = obj.getString("state");
                    String message = obj.getString("message");
                    if (state.equals(ConFigTag.state))
                    {

                        Intent intent = new Intent(RegisterActivity.this, SettingPwdActivity.class);
                        intent.putExtra("account",account);
                        intent.putExtra("input",getIntent().getStringExtra("input"));
                        startActivity(intent);

                    }else {
                        ToastUtils.showShort(message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }
            }
        });
    }


    TextWatcher textWatcher = new TextWatcher() {
        // 输入文本之前的状态
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        // 输入文本中的状态
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {


        }

        // 输入文本之后的状态
        @Override
        public void afterTextChanged(Editable s) {

            if( VerifyUtils.isEmailNO(etRegisterAccount.getText().toString().trim())){
                setIsFocusabale(true);
            }else {
                setIsFocusabale(false);
            }
        }
    };
    private void setIsFocusabale(boolean b) {

        if(b){
            iv_login_login.setFocusable(true);
            iv_login_login.setImageResource(R.mipmap.register_next_click);
        }else {
            iv_login_login.setFocusable(false);
            iv_login_login.setImageResource(R.mipmap.register_next_unclick);
        }
    }

}
